<?php

abstract class Skjb_Search_Xapian
{
    const DB_CREATE_OR_OPEN = DB_CREATE_OR_OPEN;

    const DB_CREATE = DB_CREATE;

    const DB_CREATE_OR_OVERWRITE = DB_CREATE_OR_OVERWRITE;

    const DB_OPEN = DB_OPEN;
    
    public static function load()
    {
        // Try to load our extension if it's not already loaded.
        if (!extension_loaded('xapian')) {
            if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
                if (!dl('php_xapian.dll')) {
                    return;
                }
            } else {
                // PHP_SHLIB_SUFFIX is available as of PHP 4.3.0, for older PHP assume 'so'.
                // It gives 'dylib' on MacOS X which is for libraries, modules are 'so'.
                if (PHP_SHLIB_SUFFIX === 'PHP_SHLIB_SUFFIX' || PHP_SHLIB_SUFFIX === 'dylib') {
                    if (!dl('xapian.so')) {
                        return;
                    }
                } else {
                    if (!dl('xapian.' . PHP_SHLIB_SUFFIX)) {
                        return;
                    }
                }
            }
        }
    }

    static function BAD_VALUENO_get()
    {
        return BAD_VALUENO_get();
    }

    static function versionString()
    {
        return version_string();
    }

    static function majorVersion()
    {
        return major_version();
    }

    static function minorVersion()
    {
        return minor_version();
    }

    static function revision()
    {
        return revision();
    }

    static function autoOpenStub($file)
    {
        $r = auto_open_stub($file);
        return (is_resource($r) ? new Skjb_Search_Xapian_Database($r) : $r);
    }

    static function quartzOpen($dir, $action = null, $block_size = 8192)
    {
        switch (func_num_args()) {
            case 1:
                $r = quartz_open($dir);
                break;
            default:
                $r = quartz_open($dir, $action, $block_size);
        }
        if (!is_resource($r)) {
            return $r;
        }
        switch (get_resource_type($r)) {
            case '_p_Xapian__WritableDatabase':
                return new Skjb_Search_Xapian_Database_Writable($r);
            default:
                return new Skjb_Search_Xapian_Database($r);
        }
    }

    static function flintOpen($dir, $action = null, $block_size = 8192)
    {
        switch (func_num_args()) {
            case 1:
                $r = flint_open($dir);
                break;
            default:
                $r = flint_open($dir, $action, $block_size);
        }
        if (!is_resource($r)) {
            return $r;
        }
        switch (get_resource_type($r)) {
            case '_p_Xapian__WritableDatabase':
                return new Skjb_Search_Xapian_Database_Writable($r);
            default:
                return new Skjb_Xapian_Database($r);
        }
    }

    static function inmemoryOpen()
    {
        $r = inmemory_open();
        return (is_resource($r) ? new Skjb_Search_Xapian_Database_Writable($r) : $r);
    }

    static function remoteOpen($host_or_program, $port_or_args, $timeout = 10000, $connect_timeout = null)
    {
        switch (func_num_args()) {
            case 2:
            case 3:
                $r = remote_open($host_or_program, $port_or_args, $timeout);
                break;
            default:
                $r = remote_open($host_or_program, $port_or_args, $timeout, $connect_timeout);
        }
        return (is_resource($r) ? new Skjb_Search_Xapian_Database($r) : $r);
    }

    static function remoteOpenWritable($host_or_program, $port_or_args, $timeout = 10000, $connect_timeout = null)
    {
        switch (func_num_args()) {
            case 2:
            case 3:
                $r = remote_open_writable($host_or_program, $port_or_args, $timeout);
                break;
            default:
                $r = remote_open_writable($host_or_program, $port_or_args, $timeout, $connect_timeout);
        }
        return (is_resource($r) ? new Skjb_Search_Xapian_Database_Writable($r) : $r);
    }

    static function sortableSerialise($value)
    {
        return sortable_serialise($value);
    }

    static function sortableUnserialise($value)
    {
        return sortable_unserialise($value);
    }
}